package service;

import dao.TeacherDao;
import domain.Teacher;

public class TeacherService {
    private TeacherDao teacherDao = new TeacherDao();

    // 添加老师
    public boolean addTeacher(Teacher t) {
        return teacherDao.addTeacher(t);
    }

    // 获取id信息
    public boolean isExists(String id) {
        // 1.获取库管对象中的数组
        Teacher[] teachers = teacherDao.findAllTeacher();
        boolean exsits = false;

        // 2.遍历数组，取出每一个元素，进行判断
        for (int i = 0; i < teachers.length; i++) {
            Teacher teacher = teachers[i];
            if (teacher != null && teacher.getId().equals(id)) {
                exsits = true;
                break;
            }
        }
        return exsits;
    }

    // 查询老师信息
    public Teacher[] findAllTeacher() {
        Teacher[] allTeacher = teacherDao.findAllTeacher();
        boolean flag = false;
        for (int i = 0; i < allTeacher.length; i++) {
            Teacher t = allTeacher[i];
            if (t != null) {
                // 有一个不为null，则为ture
                flag = true;
                break;
            }
        }
        if (flag) {
            return allTeacher;
        }else {
            return null;
        }
    }

    // 删除老师id
    public void deleteTeacherById(String id) {
        teacherDao.deleteTeacherById(id);
    }

    // 更新老师信息
    public void updateTeacher(String id, Teacher newTeacher) {
        teacherDao.updateTeacher(id,newTeacher);
    }
}
